/**
 * Created with IntelliJ IDEA.
 * User: work
 * Date: 15/5/10
 * Time: 下午7:51
 * To change this template use File | Settings | File Templates.
 */

var utils = utils || {};


/**
 *
 * 将库中原件指定帧生成图片，并指定存放位置
 * @param symbolItem
 * @param frameCount
 * @param fullName
 */
utils.exportAndMoveTo = function (symbolItem, frameCount, fullName) {
    if (symbolItem == null) {
        trace("symbolItem is null");
        return;
    }

    var index = fullName.lastIndexOf("/");
    var name = fullName.slice(index + 1);
    var folder = fullName.slice(0, index);
    symbolItem.exportToLibrary(frameCount, name);

    library.newFolder(folder);
    library.moveToFolder(folder, name);
};
/**
 * 获取库中的元件
 * @param fullName
 * @returns {*}
 */
utils.getSymbolItem = function (fullName) {

    var index = library.findItemIndex(fullName);
    return library.items[index];
};

/**
 * 清理库中无用的文件
 * 导出链接，在场景中未使用，不会被删除
 */
utils.cleanLibrary = function () {

    trace("=========需要删除" + library.unusedItems.length + "个文件==========");
    for each(var item in library.unusedItems) {
        trace("删除库中文件:" + item.name);
        library.deleteItem(item.name);
    }
};

/**
 * 将层移动到一个文件夹
 * @param tl
 * @param layerFolder
 */
utils.moveToLayerFolder = function (tl, layerFolder) {
    var newLayerIndex = tl.addNewLayer(layerFolder, "folder", true);
    var newLayer = tl.layers[newLayerIndex];
    tl.reorderLayer(newLayerIndex, 0);//移动到最上层
    for(var i=1; i<tl.layers.length;i++){
        if(tl.layers[i].parentLayer==null)tl.layers[i].parentLayer = newLayer;
    }
};
/**
 * 将库中的文件移动到folder文件夹中
 * @param doc
 * @param folder
 */
utils.moveToFolder = function (doc, folder) {
    var library = doc.library;
    library.newFolder(folder);
    for each (var symbolItem in library.items) {
        if (symbolItem.name == folder)continue;
        var index = symbolItem.name.indexOf("/");
        var f = "";//文件夹名称
        if (index != -1) {
            f = symbolItem.name.slice(0, index);
        }
        //在文件夹中
        if (f != "") {
            //在目标文件夹中
            if (f == folder) {

            } else {
                var childF = symbolItem.name.slice(0, symbolItem.name.lastIndexOf("/"));
                library.newFolder(folder + childF);
                library.moveToFolder(folder + childF, symbolItem.name);
            }
        } else {
            library.moveToFolder(folder, symbolItem.name);
        }

    }
};
/**
 * 批量重命名，文件夹除外
 */
utils.renameSymbolItem = function (doc) {
    var library = doc.library;
    var i = 0;
    var time = new Date().getTime();
    for each(var item in library.items) {
        if (item.itemType != "folder") {
            item.name = "rename" + (++i) + "_" + time;
        }
    }
};

/**
 * 重命名连接
 * @param doc
 */
utils.renameLinkage = function (doc) {
    var library = doc.library;
    var time = new Date().getTime();
    for each(var item in library.items) {
        if (item.linkageClassName != undefined && item.getData("hasRenameLinkage") != 1) {
            item.linkageClassName = "source." + item.linkageClassName + time;
            item.addData("hasRenameLinkage", "integer", 1);
        }
    }
};

/**
 * 获取库中有链接名的影片剪辑
 */
utils.getLinkageItems = function (doc) {

    var library = doc.library;
    trace("=========有链接的文件==========");
    var items = [];
    for each (var item in library.items) {

        if (item.linkageClassName != undefined) {
            items.push(item);
            trace("[" + item.itemType + "][" + item.linkageClassName + "]" + item.name);
        }
    }

    return items;

};


/**
 * 获取影片剪辑中的所有对象
 * @param tl
 */
utils.getSymbolInstance = function (tl) {
    var items = [];
    for each (var layer in tl.layers) {
        for each(var frame in layer.frames) {
            for each(var element in frame.elements) {
                if (items.indexOf(element) == -1) {
                    items.push(element);
                    //遍历
                    /*if(element.symbolType=="movie clip"){
                     items.splice(items.length,0,utils.getSymbolInstance(element.libraryItem.timeline));
                     }*/
                }
            }
        }
    }

    return items;

};